<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        $this->prov = @$_SESSION['district']['prov'];
        $this->city = @$_SESSION['district']['city'];
        parent::__construct();
    }
    function get_all($warr)
    {
        $shop_id = isset($warr['shop_id']) ? $warr['shop_id'] : '';
        $cid = isset($warr['cid']) ? $warr['cid'] : '';
        $group_cid = isset($warr['group_cid']) ? $warr['group_cid'] : '';
        $brand = isset($warr['brand']) ? $warr['brand'] : '';
        $attr = isset($warr['attr']) ? $warr['attr'] : '';
        $sort = isset($warr['sort']) ? $warr['sort'] : '';
        $key = isset($warr['key']) ? $warr['key'] : '';
        $dist = isset($warr['dist']) ? $warr['dist'] : '';
        $page = isset($warr['page']) ? $warr['page'] : 1;
        $page_size = isset($warr['page_size']) ? $warr['page_size'] : 32;
        $page_type = isset($warr['page_type']) ? $warr['page_type'] : 1;
        $substation = isset($warr['substation']) ? $warr['substation'] : 0;
        $cresult = $this->db->get_all("select cid,pcid,name,status from " . table('category') . " where status = 1");
        foreach ($cresult as $k => $v) {
            $cats[$v['cid']] = $v['name'];
            $cats_in[$v['pcid']][$v['cid']] = $v['name'];
        }
        $wsql = '';
        $where_attr_sql = '';
        if (!empty($shop_id)) {
            $wsql .= 'g.shop_id = ' . $shop_id . ' and';
        }
        if (!empty($cid)) {
            $catsIn = $this->cats_arr($cid, $cats_in);
            $allow_pcid = implode(',', $catsIn);
            $wsql .= ' g.cid in(' . $allow_pcid . ') and ';
            $where_attr_sql .= ' g.cid in (' . $allow_pcid . ') and ';
        }
        if (!empty($group_cid)) {
            $wsql .= ' g.group_cid = ' . $group_cid . ' and ';
            $where_attr_sql .= ' g.group_cid ' . $group_cid . ' and ';
        }
        if (!empty($brand)) {
            $wsql .= ' g.brand = ' . $brand . ' and ';
            $where_attr_sql .= ' g.brand = ' . $brand . ' and ';
        }
        if (!empty($key)) {
            $wsql .= ' g.title like "%' . $key . '%" and ';
            $where_attr_sql .= ' g.title like "%' . $key . '%" and ';
        }
        if ($this->sys['substation'] == '1') {
            if (!empty($substation)) {
                $wsql .= 'g.prov = ' . $this->prov . ' and city = ' . $this->city;
                $where_attr_sql .= 'g.prov = ' . $this->prov . ' and g.city = ' . $this->city . ' and ';
            }
            if (!empty($prov)) {
                $wsql .= 'g.prov = ' . $this->prov . ' and ';
                $where_attr_sql .= 'g.prov = ' . $this->prov . ' and ';
            }
            if (!empty($city)) {
                $wsql .= 'g.city = ' . $this->city . ' and ';
                $where_attr_sql .= 'g.city = ' . $this->city . ' and ';
            }
            if (!empty($dist)) {
                $wsql .= 'g.dist = ' . $dist . ' and ';
                $where_attr_sql .= 'g.dist = ' . $dist . ' and ';
            }
        }
        if (!empty($sort)) {
            switch ($sort) {
                case 'volume':
                    $px_sql = ' g.volume desc ';
                    $px_attr_sql = ' g.volume desc ';
                    break;
                case 'price':
                    $px_sql = ' g.price desc ';
                    $px_attr_sql = ' g.price desc ';
                    break;
                case '_price':
                    $px_sql = ' g.price asc ';
                    $px_attr_sql = ' g.price asc ';
                    break;
                case 'comment':
                    $px_sql = ' g.comment desc ';
                    $px_attr_sql = ' g.comment desc ';
                    break;
                case 'time':
                    $px_sql = ' g.addtime desc ';
                    $px_attr_sql = ' g.addtime desc ';
                    break;
                case 'px_index':
                    $px_sql = ' g.px_index desc,g.volume desc ';
                    $px_attr_sql = ' g.px_index desc,g.volume desc ';
                    break;
                case 'px_hot':
                    $px_sql = ' g.px_hot,g.volume desc ';
                    $px_attr_sql = ' g.px_hot desc,g.volume desc ';
                    break;
                case 'px_recommend':
                    $px_sql = ' g.px_recommend,g.volume desc ';
                    $px_attr_sql = ' g.px_recommend,g.volume desc ';
                    break;
                case 'id':
                    $px_sql = ' g.id desc ';
                    $px_attr_sql = ' g.id desc ';
                    break;
                default:
                    $px_sql = ' g.volume desc ';
                    $px_attr_sql = ' g.volume desc ';
            }
        } else {
            $px_sql = ' g.addtime desc ';
            $px_attr_sql = ' g.addtime desc ';
        }
        if ($attr) {
            $attr_arr = explode(',', $attr);
            $attr_num = count($attr_arr);
            $attr_sql = "select a.gid,g.id,g.cid,g.brand,g.price,g.market_price,g.volume,g.status,g.stock from " . table('goods_attr_index') . " as a right join " . table('goods') . " as g on g.id = a.gid where " . $where_attr_sql . " g.status = 1 and a.attr_id in (" . $attr . ") group by a.gid having count(distinct a.attr_id) = " . $attr_num . " order by " . $px_attr_sql . " limit " . ($page - 1) * $page_size . "," . $page_size;
            $attr_result = $this->db->get_all($attr_sql);
            if ($attr_result) {
                foreach ($attr_result as $k => $v) {
                    $gid_arr[] = $v['gid'];
                }
            }
            if (isset($gid_arr)) {
                $gid = implode(',', $gid_arr);
                $attr_goods_sql = "select g.*,s.id as shop_id,s.name as shop_name from " . table('goods') . " as g left join " . table('shop') . " as s on g.shop_id = s.id where g.id in (" . $gid . ") and g.status = 1 order by field(g.id," . $gid . ");";
                $goodsAll = $this->db->get_all($attr_goods_sql);
                $data['list'] = $this->get_one_list($goodsAll);
                $sql = "select count(*) as total from " . table('goods_attr_index') . " as a right join " . table('goods') . " as g on g.id = a.gid where " . $where_attr_sql . " a.attr_id in (" . $attr . ") group by a.gid having count(distinct a.attr_id) = " . $attr_num;
                $total = $this->db->get_one($sql);
                if ($page_size < $total['total']) {
                    $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
                }
                $data['total'] = $total['total'];
            }
        } else {
            $sql = "select g.id,g.cid,g.shop_id,g.title,g.intro,g.price,g.market_price,g.images,g.volume,g.comment,g.px_hot,g.px_recommend,g.stock,s.name as shop_name from " . table('goods') . " as g left join " . table('shop') . " as s on g.shop_id = s.id where " . $wsql . " g.status = 1 order by " . $px_sql . " limit " . ($page - 1) * $page_size . "," . $page_size;
            $goodsAll = $this->db->get_all($sql);
            $data['list'] = $this->get_one_list($goodsAll);
            $total = $this->db->get_one("select count(*) as total from " . table('goods') . " as g left join " . table('shop') . " as s on g.shop_id = s.id where " . $wsql . " g.status = 1");
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'], 'page_type' => $page_type));
            }
            $data['total'] = $total['total'];
        }
        $data['cats_name'] = $cats[$cid];
        return $data;
    }
    function cats_arr($cid, $cats, &$result = array())
    {
        if ($cid) {
            if (is_array($cats[$cid])) {
                foreach ($cats[$cid] as $k => $v) {
                    if (isset($cats[$k])) {
                        $this->cats_arr($k, $cats, $result);
                    } else {
                        $result[] = $k;
                    }
                }
            } else {
                $result[] = $cid;
            }
            return $result;
        }
    }
    private function get_one_list($goodsAll)
    {
        $time = time();
        if ($goodsAll) {
            foreach ($goodsAll as $k => $v) {
                $goods_all_in[] = $v['id'];
            }
            $goods_all_in = implode(',', $goods_all_in);
            $img_result = $this->db->get_all("select * from " . table('goods_img') . " where gid in(" . $goods_all_in . ")");
            foreach ($img_result as $key => $val) {
                $imgData[$val['gid']][] = $val['url'];
            }
            if (isset($imgData)) {
                $goods_num = count($goodsAll);
                for ($i = 0; $i < $goods_num; $i++) {
                    $goodsAll[$i]['simg'] = $imgData[$goodsAll[$i]['id']];
                    $goodsAll[$i]['cname'] = $cats[$goodsAll[$i]['cid']];
                    $goodsAll[$i]['zhe'] = round(10 / ($goodsAll[$i]['market_price'] / $goodsAll[$i]['price']), 1);
                    $goods[] = $goodsAll[$i];
                }
                return $goods;
            }
        }
    }
    public function get_one($id)
    {
        $id = isset($id) ? intval($id) : false;
        $row = $this->db->get_one("select * from " . table('goods') . " where status = 1 and id = " . $id);
        if ($row['id']) {
            $now_time = time();
            $sk_row = $this->db->get_one("select * from " . table('promote_seckill') . " where gid = " . $id . " and status = 1");
            if ($sk_row['end_time'] > $now_time) {
                $row['promote_type'] = '1';
                if ($sk_row['start_time'] < $now_time && $sk_row['end_time'] >= $now_time) {
                    $row['sk_status'] = '1';
                    $row['price'] = $sk_row['price'];
                    $row['stime_cha'] = $sk_row['start_time'] - $now_time;
                    $row['etime_cha'] = $sk_row['end_time'] - $now_time;
                } else {
                    if ($sk_row['start_time'] - 3600 * 24 < $now_time) {
                        $row['sk_status'] = '2';
                        $row['stime_cha'] = $sk_row['start_time'] - $now_time;
                        $row['etime_cha'] = $sk_row['end_time'] - $now_time;
                    }
                }
            }
            $gp_row = $this->db->get_one("select * from " . table('promote_group') . " where gid = " . $id . " and status = 1");
            if ($gp_row['start_time'] < $now_time && $gp_row['end_time'] >= $now_time) {
                $row['promote_type'] = '2';
                $row['group_price'] = $gp_row['price'];
                $row['p_number'] = $gp_row['num'];
                $row['time_cha'] = $gp_row['end_time'] - $now_time;
            }
            if ($this->uid) {
                $c_row = $this->db->get_one("select id,uid,gid from " . table('collect_goods') . " where uid = " . $this->uid . " and gid = " . $id);
                if ($c_row['id']) {
                    $row['collect_status'] = 1;
                } else {
                    $row['collect_status'] = 0;
                }
            } else {
                $row['collect_status'] = 0;
            }
            $score_total = $row['score_good'] + $row['score_average'] + $row['score_bad'];
            $row['score_total'] = $score_total;
            if ($score_total > 0) {
                $row['score_good_rate'] = round($row['score_good'] / ($row['score_good'] + $row['score_average'] + $row['score_bad']), 2);
                $row['score_average_rate'] = round($row['score_average'] / ($row['score_good'] + $row['score_average'] + $row['score_bad']), 2);
                $row['score_bad_rate'] = round($row['score_bad'] / ($row['score_good'] + $row['score_average'] + $row['score_bad']), 2);
            } else {
                $row['score_good_rate'] = '1';
                $row['score_average_rate'] = '0';
                $row['score_bad_rate'] = '0';
            }
            setcookie("goodsView[" . $id . "]", '1', time() + 3600);
            $row['content'] = $this->module('album')->img_content($row['content']);
            return $row;
        } else {
            error_404('该产品已经下架');
        }
    }
    function goods_simg($id)
    {
        $result = $this->db->get_all("select * from " . table('goods_img') . " where gid = " . $id);
        foreach ($result as $key => $val) {
            $data[] = $val;
        }
        return $data;
    }
    function browse($arr)
    {
        $id = !empty($arr['id']) ? $arr['id'] : '';
        if (!empty($id)) {
            $this->db->query("update " . table('goods') . " set `click` = `click`+1 where id = " . $id);
        }
    }
    function keyword($arr)
    {
        $limit = !empty($arr['limit']) ? $arr['limit'] : '5';
        return $this->db->get_all("select id,keyword,hot,px from " . table('keyword') . " order by px desc limit " . $limit);
    }
    function comment($setArr)
    {
        $page = !empty($setArr['page']) ? $setArr['page'] : '1';
        $page_size = !empty($setArr['page_size']) ? $setArr['page_size'] : '10';
        $goods_id = !empty($setArr['goods_id']) ? $setArr['goods_id'] : exit('GOODS_ID参数错误');
        $commendCid = isset($setArr['ccid']) ? $setArr['ccid'] : '';
        if (!empty($commendCid)) {
            if ($commendCid == '1') {
                $whereSql = ' and c.score in(4,5) ';
                $whereNum = ' and score in(4,5) ';
            } elseif ($commendCid == '2') {
                $whereSql = ' and c.score in(2,3) ';
                $whereNum = ' and score in(2,3) ';
            } elseif ($commendCid == '3') {
                $whereSql = ' and c.score in(1)';
                $whereNum = ' and score in(1)';
            } else {
                $whereSql = ' ';
                $whereNum = ' ';
            }
        }
        $sql = "select c.*,u.nick,u.uid,u.avatar,g.gid,g.sku_value,g.buy_num from " . table('goods_comment') . " as c right join " . table('member') . " as u on u.uid = c.uid right join " . table('order_goods') . " as g on c.oid = g.oid where g.gid =  " . $goods_id . " and c.gid = " . $goods_id . ' ' . $whereSql . " order by c.id desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $comment_query = $this->db->get_all($sql);
        if ($comment_query) {
            foreach ($comment_query as $k => $v) {
                $comment_img_arr[] = $v['id'];
            }
            if ($comment_img_arr) {
                $comment_img_in = implode(',', $comment_img_arr);
                $img_all = $this->db->get_all("select * from " . table('goods_comment_img') . " where comment_id in (" . $comment_img_in . ")");
                if ($img_all) {
                    foreach ($img_all as $k => $v) {
                        $comment_img[$v['comment_id']][] = $v['images'];
                    }
                }
            }
            foreach ($comment_query as $k => $v) {
                if ($v['nick']) {
                    $v['nick'] = hideCenterStr($v['nick']);
                } else {
                    $v['nick'] = '匿**名';
                }
                $v['sku_value'] = unserialize($v['sku_value']);
                $v['comment_img'] = $comment_img[$v['id']];
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('goods_comment') . " where gid = " . $goods_id . ' ' . $whereNum);
            $data['total'] = $total['total'];
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'total' => $total['total'], 'page_size' => $page_size));
                $data['next_page'] = $this->uqcms_next_page(array('page' => $page, 'total' => $total['total'], 'page_size' => $page_size));
            }
            return $data;
        } else {
            return false;
        }
    }
    function get_brand($cid)
    {
        if ($cid) {
            $wcid = 'cid in (0, ' . $cid . ')';
            $data = $this->db->get_all("select * from " . table('goods_brand') . " where " . $wcid . " and status = 1 order by px desc");
            return $data;
        } else {
            return false;
        }
    }
    public function get_attr_list($id)
    {
        $result = $this->db->get_all("select * from " . table('goods_attr_index') . " where gid = " . $id);
        foreach ($result as $k => $v) {
            if ($v['attr_id']) {
                $attr_arr[] = $v['attr_id'];
            }
        }
        if (count($attr_arr) != '0') {
            $attr_id = implode(',', $attr_arr);
            $aquery = $this->db->query("select * from " . table('category_attr') . " where id in(" . $attr_id . ") ");
            while ($arow = $this->db->fetch_array($aquery)) {
                $attr_id_f[] = $arow['pid'];
                $attr_name[$arow['pid']] = $arow['name'];
            }
            if (count($attr_id_f) != 0) {
                $attr_id_f = implode(',', $attr_id_f);
                $resultQuery = $this->db->query("select * from " . table('category_attr') . " where id in(" . $attr_id_f . ") ");
                while ($resultRow = $this->db->fetch_array($resultQuery)) {
                    $data[] = array('name' => $resultRow['name'], 'value' => $attr_name[$resultRow['id']]);
                }
                return $data;
            }
        }
    }
    function shop_goods($arr)
    {
        $shop_id = isset($arr['shop_id']) ? $arr['shop_id'] : '';
        $group_cid = isset($arr['group_cid']) ? $arr['group_cid'] : '';
        $sort = isset($arr['sort']) ? $arr['sort'] : '';
        $page = isset($arr['page']) ? $arr['page'] : '1';
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : '20';
        $now_time = time();
        $new_time = time() - 3600 * 24 * 7;
        $sp_wsql = " where shop_id = " . $shop_id . " and start_time <=" . $now_time . " and end_time >= " . $now_time . " and status = 1";
        if ($sort == 'sps') {
            $og_all = $this->db->get_all("select * from " . table('promote_seckill') . $sp_wsql . ' limit ' . ($page - 1) * $page_size . ',' . $page_size);
            if ($og_all) {
                foreach ($og_all as $k => $v) {
                    $gin_arr[] = $v['gid'];
                }
                $gs_in = implode(',', $gin_arr);
                $all = $this->db->get_all("select * from " . table('goods') . " where status = 1 and id in (" . $gs_in . ")");
                if ($all) {
                    foreach ($all as $k => $v) {
                        $list[] = $v;
                    }
                    $data['list'] = $list;
                }
            }
        } else {
            if ($shop_id) {
                $wArr[] = 'shop_id = ' . $shop_id;
            }
            if ($group_cid) {
                $wArr[] = 'group_cid = ' . $group_cid;
            }
            if ($sort == 'new') {
                $wArr[] = 'addtime > ' . $new_time;
            }
            if (isset($wArr)) {
                $wsql = ' and ' . implode(' and ', $wArr);
            }
            $all = $this->db->get_all("select * from " . table('goods') . " where status = 1 " . $wsql . " limit " . ($page - 1) * $page_size . ", " . $page_size);
            if ($all) {
                foreach ($all as $k => $v) {
                    $list[] = $v;
                }
                $data['list'] = $list;
            }
        }
        $total = $this->db->get_one("select count(id) as total from " . table('goods') . " where status = 1 and  shop_id = " . $shop_id);
        $new = $this->db->get_one("select count(id) as total from " . table('goods') . " where status = 1 and shop_id = " . $shop_id . " and addtime > " . $new_time);
        $sps = $this->db->get_one("select count(id) as total from " . table('promote_seckill') . $sp_wsql);
        $data['count']['total'] = $total['total'];
        $data['count']['new'] = $new['total'];
        $data['count']['sps'] = $sps['total'];
        return $data;
    }
    public function goods_hot($arr)
    {
        $gid = isset($arr['gid']) ? $arr['gid'] : '0';
        $spid = isset($arr['spid']) ? $arr['spid'] : '0';
        $limit = $arr['limit'] ? $arr['limit'] : '20';
        $wsql = '';
        if ($gid) {
            $gone = $this->db->get_one("select id,shop_id from " . table('goods') . " where status = 1 and id = " . $gid . " limit 1");
            $spid = $gone['shop_id'];
            $wsql = " where shop_id = " . $spid . " and id != " . $gid;
        } elseif ($spid) {
            $wsql = " where shop_id = " . $spid;
        }
        $data = $this->db->get_all("select id,images,title,price,market_price,volume from " . table('goods') . $wsql . " order by volume desc limit " . $limit);
        if ($data) {
            return $data;
        } else {
            return false;
        }
    }
    public function goods_collect($arr)
    {
        $spid = $arr['spid'] ? $arr['spid'] : '';
        $limit = $arr['limit'] ? $arr['limit'] : '5';
        return $this->db->get_all("select * from " . table('goods') . " where shop_id = " . $spid . " order by collect desc limit " . $limit);
    }
    public function goods_histroy()
    {
        $view = isset($_COOKIE['goodsView']) ? $_COOKIE['goodsView'] : false;
        if ($view) {
            $view_num = count($view);
            if ($view_num != '0') {
                foreach ($view as $k => $v) {
                    if ($k != '0') {
                        $viewIn[] = $k;
                    }
                }
                $viewIn = implode($viewIn, ',');
                if (!empty($viewIn)) {
                    $data['list'] = $this->db->get_all("select id,images,title,price,market_price,volume from " . table('goods') . " where id in(" . $viewIn . ")");
                    $data['total'] = $view_num;
                    return $data;
                }
            }
        } else {
            return false;
        }
    }
    public function collect($arr)
    {
        $page = isset($arr['page']) ? $arr['page'] : 1;
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : 30;
        $page_type = isset($arr['page_type']) ? $arr['page_type'] : 1;
        $sql = "select c.*,g.id as gid,g.title,g.images,g.price,g.market_price from " . table('collect_goods') . " as c right join " . table('goods') . " as g on g.id = c.gid where c.uid = " . $this->uid . " order by c.addtime desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $result['list'] = $this->db->get_all($sql);
        $total = $this->db->get_one("select count(id) as total from " . table('collect_goods') . " where uid = " . $this->uid);
        $result['total'] = $total;
        if ($page_size < $total['total']) {
            $result['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'], 'page_type' => $page_type));
        }
        return $result;
    }
    public function group($shop_id)
    {
        return $this->db->get_all("select * from " . table('goods_group') . " where shop_id = " . $shop_id . " order by px desc");
    }
}